package com.wms.test;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Repository;

import com.wms.dataSource.dSource;


@Repository
public class CargoDao{
	
	private SqlSessionFactory ssf;
	
	@Resource
	private dSource ds;
	
	public void setDs(dSource ds) {
		this.ds = ds;
	}

	public void setSsf(SqlSessionFactory ssf) {
		this.ssf = ssf;
	}
	
	@PostConstruct
	@Lazy
	public void init(){
		System.out.println("******* init function *******");
		setSsf(ds.initinalDataSource(this.getClass().getName()));
	}
	
	
	public List<Cargo> getCargo(){
		
		List<Cargo> list = ssf.openSession().selectList("demo.selectCargo");
		
		return list;
	}
	
	
	public void insertSku(String whseid) throws Exception{
		
		if(whseid.endsWith("miharuE")){
			setSsf(ds.getSsf1());
		}else{
			setSsf(ds.getSsf2());
		}
		
		Cargo domain = new Cargo();
		domain.setWhseid(whseid);
		
		for(int i=0; i< 5000; i++){
			
//			if(i==7 && whseid.endsWith("miharu1")){
//				throw new Exception("break down");
//			}
			domain.setSku("XPERIA"+i);
			domain.setStorerkey("SONY"+i);
			ssf.openSession().insert("insertSku", domain);
		}
	}
	
	

}
